package logic

import (
	"context"
	"fmt"
	"user_srv/models"

	"user_srv/internal/svc"
	"user_srv/user_srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type FindUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFindUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindUserLogic {
	return &FindUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FindUserLogic) FindUser(in *user_srv.FindUserRequest) (*user_srv.FindUserResponse, error) {
	// todo: add your logic here and delete this line
	var UserDate struct {
		Phone    string `gorm:"type:varchar(20)"`
		UserName string `gorm:"type:varchar(20)"`
		models.UserLdentity
	}
	if models.DB.Model(&models.User{}).Joins("join user_ldentities on users.id = user_ldentities.user_id").Where("users.id = ?", in.UserId).Select("user_ldentities.*,users.phone,users.user_name").Scan(&UserDate).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}

	return &user_srv.FindUserResponse{
		UserId:    int64(UserDate.UserId),
		CreatedAt: UserDate.CreatedAt.String(),
		Phone:     UserDate.Phone,
		RealName:  UserDate.RealName,
		IdCard:    UserDate.IdCard,
		Address:   UserDate.Province + UserDate.City + UserDate.District,
		Sex:       UserDate.Sex,
		Birthday:  UserDate.Birthday,
	}, nil
}
